<?php

namespace app\home\controller;

use think\Controller;
use think\Db;

class Home extends Base
{
    public function index()
    {

        $hot_list = Db::name('goods_group')->where(array('pid' => 0))->limit(5)->select();
        foreach ($hot_list as $key => $value) {
            $group_ids = Db::name('goods_group')->where(array('pid'=>$value['id']))->column('id');
            $group_id = implode(',', $group_ids);
            $hot_list[$key]['photo_hot'] = Db::name('user')
                ->where(array('pid' => array('in',$group_id), 'end_time' => array('>', time())))
                ->order('sort desc ,create_time desc')
                ->limit(5)
                ->select();

        }
        $list = Db::name('goods_group')
            ->where(array('pid' => 0))
            ->select();

        foreach ($list as $key => $value) {
            $group_ids = Db::name('goods_group')->where(array('pid'=>$value['id']))->column('id');
            $group_id = implode(',', $group_ids);
            $num = $key + 1;
            $str_top = "广告_" . $num . "F_商品分类广告";

            $str_left = "广告_" . $num . "L_商品分类广告";
            $list[$key]['photo'] = Db::name('user')
                ->where(array('pid' => array('in',$group_id), 'end_time' => array('>', time())))
                ->order('sort desc ,create_time desc')
                ->select();

            $list[$key]['ad_top'] = Db::name('ads')
                ->alias('a')
                ->field('a.id,a.link,a.img_url,a.title')
                ->join('ad_position p', 'p.id = a.place')
                ->where(array('p.name' => array('like', $str_top), 'p.status' => '1', 'a.status' => 1))
                ->limit($key * 2, 2)
                ->select();

            $list[$key]['ad_left'] = Db::name('ads')
                ->alias('a')
                ->field('a.id,a.link,a.img_url,a.title')
                ->join('ad_position p', 'p.id = a.place')
                ->where(array('p.name' => array('like', $str_left), 'p.status' => '1', 'a.status' => 1))
                ->limit($key * 2, 2)
                ->select();
        }

        $xie_list = Db::name('goods_group')
            ->where(array('name' => array('like', '%鞋%'), 'pid' => '0'))
            ->find();

        $xie_list['shops'] = Db::name('user')
            ->where(array('pid' => $xie_list['id'], 'end_time' => array('>', time())))
            ->order('sort desc,create_time desc')
            ->select();

        $articles = Db::name('posts')
            ->where(array('post_type' => 2, 'post_status' => 1))
            ->limit(14)
            ->select();

        $map = array(
            'hot_list' => $hot_list,
            'list' => $list,
            'xie_list' => $xie_list,
            'articles' => $articles,
        );

        return view('index', $map);

    }

    public function search()
    {

        $title = input('s');
        $type = input('d');
        if ($type == 1) {
            $where['u.name'] = array('like', $title);
        } elseif ($type == 2) {
            $where['a.name'] = array('like', $title);
        }
        $where['end_time'] = array('<', time());
        $num = Db::name('user')
            ->alias('u')
            ->field('a.id as aid,u.id as uid,a.name as aname,u.name as uname,u.qq,a.img_url,a.create_time')
            ->join('album a', 'u.id = a.uid')
            ->where($where)
            ->count();
        $pageObject = new \Page($num);

        $list = Db::name('user')
            ->alias('u')
            ->field('a.id as aid,u.id as uid,a.name as aname,u.name as uname,u.qq,a.img_url,a.create_time')
            ->join('album a', 'u.id = a.uid')
            ->where($where)
            ->page('0,10')
            ->order('u.sort desc')
            ->select();
        foreach ($list as $key => $value) {
            if (!$value['img_url']) {
                $img_url = Db::name('photo')->where(array('album_id' => $value['aid']))->find();
                $list[$key]['img_url'] = $img_url['img_url'];
            }
        }
        $map = array(
            'list' => $list,
            'num' => $num,
            'page' => $pageObject->showPages(4),
            'title' => $title
        );

        return view('search', $map);

    }

}